1 =============================================================================
2 CONSOLE APPLICATION : CppLoadLibrary Project Overview
3 =============================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 This is an example of dynamically loading a DLL using the APIs LoadLibrary,
9 GetProcAddress and FreeLibrary. In contrast with implicit linking (static
10 loading), dynamic loading does not require the LIB file, and the application
11 loads the module just before we call a function in the DLL. The API functions
12 LoadLibrary and GetProcAddress are used to load the DLL and then retrieve the
13 address of a function in the export table. Because we explicitly invoke these
14 APIs, this kind of loading is also referred to as explicit linking.
17 /////////////////////////////////////////////////////////////////////////////
20 CppLoadLibrary -> CppDynamicLinkLibrary
21 CppLoadLibrary dynamically loads CppDynamicLinkLibrary.dll and calls the
22 functions exported by the DLL.
24 CppLoadLibrary - CSLoadLibrary
25 CSLoadLibrary in C# mimics the behavior of CppLoadLibrary to dynamically load
26 a native DLL, get the address of a function in the export table, and call it.
29 /////////////////////////////////////////////////////////////////////////////
32 1. Type-define the functions exported from the DLL. For example:
34 typedef int (* LPFNGETSTRINGLENGTH1) (PWSTR);
36 2. Dynamically load the DLL by calling LoadLibrary.
38 hModule = LoadLibrary(pszModuleName);
40 3. Call GetProcAddress to get the address of the function in the export table
43 LPFNGETSTRINGLENGTH1 lpfnGetStringLength1 = (LPFNGETSTRINGLENGTH1)
44 GetProcAddress(hModule, "GetStringLength1");
48 PWSTR pszString = L"HelloWorld";
50 nLength = lpfnGetStringLength1(pszString);
52 5. Unload the library by calling FreeLibrary.
57 /////////////////////////////////////////////////////////////////////////////
60 MSDN: Using Run-Time Dynamic Linking
61 http://msdn.microsoft.com/en-us/library/ms686944.aspx
64 /////////////////////////////////////////////////////////////////////////////